# -*- coding: utf-8 -*- 

import wx
import wx.xrc
import Database

###########################################################################
## Class CategoryDialog
###########################################################################

class CategoryDialog ( wx.Dialog ):
    
    def __init__( self, parent ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"产品分类定义", pos = wx.DefaultPosition, size = wx.Size( 600,420 ), style = wx.DEFAULT_DIALOG_STYLE )
        
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        
        bSizer2 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_lcList = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LC_REPORT )
        self.m_lcList.InsertColumn(0, u"编号")
        self.m_lcList.InsertColumn(1, u"全名")
        self.m_lcList.InsertColumn(2, u"简称")
        bSizer2.Add( self.m_lcList, 1, wx.ALL|wx.EXPAND, 5 )
        
        bSizer1.Add( bSizer2, 1, wx.EXPAND, 5 )
        
        self.m_staticline1 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer1.Add( self.m_staticline1, 0, wx.EXPAND |wx.ALL, 5 )
        
        bSizer33 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_btnNew = wx.Button( self, wx.ID_ANY, u"新建", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer33.Add( self.m_btnNew, 0, wx.ALL, 5 )
        
        self.m_btnDelete = wx.Button( self, wx.ID_ANY, u"删除", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer33.Add( self.m_btnDelete, 0, wx.ALL, 5 )
        
        self.m_btnEdit = wx.Button( self, wx.ID_ANY, u"修改", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer33.Add( self.m_btnEdit, 0, wx.ALL, 5 )
        
        self.m_btnSubmit = wx.Button( self, wx.ID_ANY, u"提交", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer33.Add( self.m_btnSubmit, 0, wx.ALL, 5 )
        
        bSizer1.Add( bSizer33, 0, wx.EXPAND, 5 )
        
        sbSizer7 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"内容" ), wx.VERTICAL )
        
        fgSizer10 = wx.FlexGridSizer( 0, 6, 0, 0 )
        fgSizer10.SetFlexibleDirection( wx.BOTH )
        fgSizer10.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        
        self.m_staticText50 = wx.StaticText( self, wx.ID_ANY, u"编号", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText50.Wrap( -1 )
        fgSizer10.Add( self.m_staticText50, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcNo = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcNo.Enable( False )
        
        fgSizer10.Add( self.m_tcNo, 0, wx.ALL, 5 )
        
        self.m_staticText51 = wx.StaticText( self, wx.ID_ANY, u"全名", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText51.Wrap( -1 )
        fgSizer10.Add( self.m_staticText51, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcFullName = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcFullName.Enable( False )
        
        fgSizer10.Add( self.m_tcFullName, 0, wx.ALL, 5 )
        
        self.m_staticText52 = wx.StaticText( self, wx.ID_ANY, u"简称", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText52.Wrap( -1 )
        fgSizer10.Add( self.m_staticText52, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcShortName = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcShortName.Enable( False )
        
        fgSizer10.Add( self.m_tcShortName, 0, wx.ALL, 5 )
        
        sbSizer7.Add( fgSizer10, 1, wx.EXPAND, 5 )
        
        bSizer1.Add( sbSizer7, 0, wx.EXPAND, 5 )
        
        self.m_staticline19 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer1.Add( self.m_staticline19, 0, wx.EXPAND |wx.ALL, 5 )
        
        m_sdbSizer1 = wx.StdDialogButtonSizer()
        self.m_sdbSizer1OK = wx.Button( self, wx.ID_OK, label = u"关闭" )
        m_sdbSizer1.AddButton( self.m_sdbSizer1OK )
        m_sdbSizer1.Realize();
        bSizer1.Add( m_sdbSizer1, 0, wx.EXPAND, 5 )
        
        self.SetSizer( bSizer1 )
        self.Layout()
        
        self.Centre( wx.BOTH )
        
        # Connect Events
        self.m_lcList.Bind( wx.EVT_LIST_ITEM_SELECTED, self.OnListItemSelected )
        self.m_btnNew.Bind( wx.EVT_BUTTON, self.OnNew )
        self.m_btnDelete.Bind( wx.EVT_BUTTON, self.OnDelete )
        self.m_btnEdit.Bind( wx.EVT_BUTTON, self.OnEdit )
        self.m_btnSubmit.Bind( wx.EVT_BUTTON, self.OnSubmit )
        
        self.currentitem = None
        self.dict = {}
        self._Init()
    
    def __del__( self ):
        pass
    
    
    # Virtual event handlers, overide them in your derived class
    def OnListItemSelected( self, event ):
        self.currentitem = item = event.m_itemIndex
        self.m_tcNo.SetValue(self.m_lcList.GetItemText(item))
        self.m_tcFullName.SetValue(self.m_lcList.GetItem(item, 1).GetText())
        self.m_tcShortName.SetValue(self.m_lcList.GetItem(item, 2).GetText())
        event.Skip()
    
    def OnNew( self, event ):
        self.m_tcNo.SetValue(u"")
        self.m_tcFullName.SetValue(u"")
        self.m_tcFullName.Enable(True)
        self.m_tcShortName.SetValue(u"")
        self.m_tcShortName.Enable(True)
        event.Skip()
    
    def OnDelete( self, event ):
        if self.currentitem == None:
            wx.MessageBox(u"请选择需要删除的条目")
            return
        
        if wx.MessageBox(u"你确定需要删除吗？一旦删除您将不能恢复", u"提示", wx.YES_NO) == wx.YES:
            index = int(self.m_tcNo.GetValue())
            Database.MPartCategory.deleteBy(id = index)
            self.m_lcList.DeleteItem(self.currentitem)
            del self.dict[index]
            self.currentitem = None
            
            self.m_tcNo.SetValue(u"")
            self.m_tcFullName.SetValue(u"")
            self.m_tcFullName.Enable(False)
            self.m_tcShortName.SetValue(u"")
            self.m_tcShortName.Enable(False)
        event.Skip()
    
    def OnEdit( self, event ):
        if self.currentitem == None:
            wx.MessageBox(u"请选择需要删除的条目")
            return
    
        self.m_tcFullName.Enable(True)
        self.m_tcShortName.Enable(True)
        event.Skip()
    
    def OnSubmit( self, event ):
        if self.m_tcNo.GetValue() == wx.EmptyString:
            r = Database.MPartCategory(name = self.m_tcFullName.GetValue(),
                                  shortname = len(self.m_tcShortName.GetValue())==0 and self.m_tcShortName.GetValue() or None)
            self._InsertItem(r)
        else:
            r = Database.MPartCategory.get(id = int(self.m_tcNo.GetValue()))
            r.name = self.m_tcFullName.GetValue()
            r.shortname = self.m_tcShortName.GetValue().encode("ascii")
            
            self._UpdateItem(r)
            
        self.m_tcFullName.Enable(False)
        self.m_tcShortName.Enable(False)
        event.Skip()
        
    def _Init(self):
        res = Database.MPartCategory.select()
        for r in res:
            self._InsertItem(r)
    
    def _InsertItem(self, r):
        item = self.m_lcList.InsertStringItem(self.m_lcList.GetItemCount(), '%d' % r.id)
        self.m_lcList.SetStringItem(item, 1, r.name)
        try:
            self.m_lcList.SetStringItem(item, 2, r.shortname)
        except:
            pass
        self.dict[r.id] = item
        
    def _UpdateItem(self, r):
        item = self.dict[r.id]
        self.m_lcList.SetStringItem(item, 1, r.name)
        self.m_lcList.SetStringItem(item, 2, r.shortname)